<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class ActivityMap extends CI_Controller 
{
	private $FB;
	public function index()
	{
	$fb_config = array(
            'appId'  => '159703560804912',
            'secret' => 'aa4895d729ddfabf9c65395cf0be6390'
        );

        $this->load->library('facebook', $fb_config);
		$this->FB=$this->facebook;
        $user = $this->facebook->getUser();

        if ($user) {
            try {
                $data['user_profile'] = $this->facebook->api('/me');
                $data['facebook']=$this->facebook;
            } catch (FacebookApiException $e) {
                $user = null;
            }
        }

        if ($user) {
            $data['logout_url'] = $this->facebook->getLogoutUrl();
        } else {
            $data['login_url'] = $this->facebook->getLoginUrl(array( 'scope' => 'publish_stream,friends_events' ));
        }
		$this->load->view('activitymap_view',$data);
	}
	function AJAX_update_places_facebook()
	{
			if(!isset($_POST['lat'])||!isset($_POST['lng'])||!isset($_POST['ratio']))
				return;	
			$lat=$_POST['lat'];
			$lng=$_POST['lng'];
			$ratio=$_POST['ratio'];
		

			//$a='-33.03551638333,-71.595061266667';
			
			$fb_config = array(
            'appId'  => '159703560804912',
            'secret' => 'aa4895d729ddfabf9c65395cf0be6390'
	        );
	
	        $this->load->library('facebook', $fb_config);
		
			$params = array(
		    'method' => 'fql.query',
	    	'scope' => 'user_status,publish_stream,user_photos',
		    'query' => "SELECT name,latitude,longitude FROM place WHERE distance(latitude, longitude,'$lat','$lng') < '$ratio'",
			);
		
			$result = $this->facebook->api($params);
			echo json_encode($result);	
	}
	function AJAX_update_events_facebook()
	{

			$fb_config = array(
            'appId'  => '159703560804912',
            'secret' => 'aa4895d729ddfabf9c65395cf0be6390'
	        );
	
	        $this->load->library('facebook', $fb_config);
		/*
			$params = array(
		    'method' => 'fql.query',
	    	'scope' => 'user_status,publish_stream,user_photos',
		    'query' => "SELECT eid, name, venue, location, pic_square, start_time FROM event WHERE eid  in
            (SELECT eid FROM event_member
            WHERE uid = me())",
			);*/
	        
	        $params = array(
		    'method' => 'fql.query',
	    	'scope' => 'user_status,publish_stream,user_photos',
		    'query' => "SELECT eid, name, venue, location, pic_square, start_time FROM event WHERE eid  in
            (SELECT eid FROM event_member
            WHERE uid in (SELECT uid2 from friend WHERE uid1=me() ))",
			);
	        /*
	       $params = array(
		    'method' => 'fql.query',
	    	'scope' => 'user_status,publish_stream,user_photos',
		    'query' => "SELECT uid2 from friend WHERE uid1=me()",
			);*/
	        /*
	        $params = array(
		    'method' => 'fql.query',
	    	'scope' => 'user_status,publish_stream,user_photos',
		    'query' => "SELECT eid, end_time FROM event WHERE eid IN (SELECT eid FROM event_member WHERE  uid IN (SELECT uid FROM user WHERE uid = me() OR uid IN (SELECT uid2 FROM friend WHERE uid1 = me()))) 
	        ",
			);*/
			
	$result = $this->facebook->api($params);
		/*	echo "<pre>";
			echo print_r($result);	
			echo "</pre>";*/
	
			echo json_encode($result);
				
	}
	
}